﻿using Microsoft.AspNetCore.Http;
using Models.Common;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace GatewayServerService.Data
{
    public class DBIotDeviceTaskDefaultRun
    {


        EFYihongIotContext ef = new EFYihongIotContext();
        public iot_device_task_default_run getModel(string guid)
        {
            return ef.iot_device_task_default_run.Where(c => c.dr_guid == guid).FirstOrDefault();
        }




        public bool saveAll(IFormCollection FC, HttpReqModel httpRequest, MagerLoginInfo LoginUserInfo, iot_device_task_default_run req)
        {
            //先查一下有没重复提交
            iot_device_task_default_run model = ef.iot_device_task_default_run.Where(c => c.dr_guid == req.dr_guid).FirstOrDefault();
            if (model == null)
            {

                //写入
                req.dr_add_time = DateTime.Now;
                req.dr_mod_time = DateTime.Now;
                req.dr_start_time = DateTime.Now;
                req.dr_interval_name = "秒";
                req.dr_interval_unit = "ss";
                req.dr_complete = 0;
                req.dr_execute_count = 0;
                req.dr_c_guid = LoginUserInfo.userCompanyGuid;
                ef.iot_device_task_default_run.Add(req);
                ef.SaveChanges();
            }
            else
            {
                model.dr_title = req.dr_title;
                model.dr_status = req.dr_status;
                model.dr_interval_value = req.dr_interval_value;
                model.dr_key = req.dr_key;
                model.dr_d_guid = req.dr_d_guid;
                model.dr_repeat_count = req.dr_repeat_count;
                model.dr_title = req.dr_title;
                model.dr_mod_time = DateTime.Now;
                ef.iot_device_task_default_run.Update(model);
                ef.SaveChanges();
            }
            return true;
        }
    }
}
